package com.onePractice.spring.demo.controller;

import com.onePractice.spring.demo.model.MessageInfo;
import com.onePractice.spring.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @RequestMapping(value = "/publish",produces = "application/json")
    @PostMapping(value = "/publish",produces = "application/json")
    public String publish(@RequestBody MessageInfo messageInfo) {
        if(!StringUtils.hasLength(messageInfo.getFrom())
                || !StringUtils.hasLength(messageInfo.getMessage())
                || !StringUtils.hasLength(messageInfo.getTo())) {
            return "{\"ok\": 0}";
        }
        messageService.addMessage(messageInfo);
//        messageInfoList.add(messageInfo);
        return "{\"ok\": 1}";
    }

//    @GetMapping("/getList")
//    public List<MessageInfo> getList() {
//        return messageInfoList;
//    }
}
